package com.picturetransform;

import java.io.IOException;

import android.content.Context;
import android.hardware.Camera;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.widget.Button;

public class CameraView extends SurfaceView implements SurfaceHolder.Callback {

	SurfaceHolder mHolder;
	Camera mCamera;
	Context mContext;
	Button captureButton;
	static final int FOTO_MODE = 0;
	
	public CameraView(Context context,Camera camera) {
		super(context);
		
		mCamera = camera;
		mContext = context;
		
		mHolder = getHolder();
		mHolder.addCallback(this);
		mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
	}

	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width,
			int height) {
		
		
		if(mHolder.getSurface()==null){
			return;
		}
		try{
			mCamera.stopPreview();
		}catch(Exception e)
		{
			e.printStackTrace();
		}
		
		try{
			
			Camera.Parameters parameters = mCamera.getParameters();
			mCamera.setParameters(parameters);
			mCamera.setPreviewDisplay(holder);

			mCamera.startPreview();
		}catch(Exception e){
			e.printStackTrace();
		}
	}

	@Override
	public void surfaceCreated(SurfaceHolder holder) {
		try {
			mCamera.setPreviewDisplay(holder);
			mCamera.startPreview();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}

	
	
	@Override
	public void surfaceDestroyed(SurfaceHolder holder) {
		if(mCamera!=null)
			mCamera.stopPreview();
	}
	
	
	

}
